Lesson #4:
Download a new program DrScheme and have fun.
Choose the program language - Teaching language - Intermediate Student.
Play a little bit with the basic function in the down part. DrSchem the begining. How to designe the program.
Next step is to try out the uper part.
Write: (define(twice x)
(* 2 x))
(twice 5)
(* 2 (+ 4 6))
(+ 1 2 3)
and check out function like Step , Check Syntax, Run, Stop.
If you need some help click on help.
Other examples: (sqrt (+ (expt 10 2) (expt 15 2)))
or
(< (expt 2 10) (expt 10 2))
or
(max (expt 2 10) (expt 10 2))
or
(list (expt 2 10) (expt 10 2))
(define (quadeq a b c)
(list (root1 a b c)
(root2 a b c)))
(define (root1 a b c)
(/ (+ (- b) (sqrt (discrim a b c))) (* 2 a)))
(define (root2 a b c)
(/ (- (- b) (sqrt (discrim a b c))) (* 2 a)))
(define (discrim a b c)
(- (* b b) (* 4 a c)))
(quadeq 1 1 -12)
... result= (list 3 -4)
(define (hypothenuse a b)
(sqrt (+ (* a a) (* b b))))
(hypothenuse 3 4)
HW task!
-create a function (last L) returning the last element of the list L.. DONT USE THE REVERSE FUNCTION
Lesson #6:
Examples:
(define-struct ware (name price amount))
(define depot (list (make-ware "Coke" 12 100)
(make-ware "Pepsi" 11 200)
(make-ware "Sprite" 10 300)))
(define (find-price name L)
(cond
[(empty? L) 0]
[(string=? name (ware-name (car L))) (ware-price (car L))]
[else (find-price name (cdr L))]
))
;;Create a function named depot-value which calculates the global value of all wares
;;in the depot
(define (depot-value L)
(cond
[(empty? L) 0]
[else (+ (global-price (car L))
(depot-value (cdr L)))]
))
(define (global-price W)
(* (ware-price W) (ware-amount W)))
(find-price "Coke" depot)
(depot-value depot)
Results 12
6400
HW task!
-create a function
; create function (bill-price order), the order is a list of lists
; (("apple" 2) ("tomato" 1)) - single sublist for every ware, contains the name ans the
; number of pieces purchased
Demo exam:
1.
Create function (f a b) evaluateing
((a/b) + (b/a)) /(a*b)
2.
Create function (select n L) selecting n/th element
from list L (if n> length (L) return #f)
3.
We have following definition of department:
(define-stuct dept (name employees))
where department name is a string and employees is a list of strings containing names of employees working in the departmet. Create function (isemployee name department) which returns #t if department contains employee with the given name.
Results:
;1 Task
(define (f a b)
(/ (+ (/ a b) (/ a b)) (* a b)))
(f 4 2)
;should be 0.5
;2 Task
(define (select n L)
(cond
[(empty? L) "the number is not in the list"]
[(> n (length L)) ((length L))]
[(<= n 1) n) (car L)]
[else (select (- n 1) (cdr L))]
))
(select 7 '( 2 3 4 5 6 7))
;3 Task
(define-struct dept (name employees))
(define (isempoyee name department)
(searchstring name ( dept-employees department)))
(define (searchstring s L)
(cond
[(emty? L)#f]
[(string=? s (car L)) #t]
[else (searchstring s (cdr))]
))
Doc. RNDr. Pavel Satrapa, Ph. D.
Email: pavel.satrapa (at) tul.cz
Tel: +420 48 535 3685
Office: A9
Some useful links for our PAP course: (Satrapa's presentations)
Programming paradigms 1
http://www.kai.vslib.cz/~satrapa/en/programming/
Programming paradigms 2
http://www.kai.vslib.cz/~satrapa/en/paradigms/
Links to Scheme Resources
http://scheme.com/resources.html
http://schemers.org/
The Scheme-Programming Language
R. Kent Dybvig
http://scheme.com/tspl3/
Lesson #1:
Modules (units)
Lesson #2:
Objects (OOP)
Lesson #3:
(SWI-Prolog)
parent(alfons,frank).
parent(alfons,vera).
parent(emily,frank).
parent(emily,vera).
parent(frank,bob).
grandparent(alfons,bob).
parent(ann,nicol).
parent(ann,mark).
parent(mark,jane).
kokot(mark,margoska).
grandparent(X,Y) :- parent(X,Z), parent(Z,Y).
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).
Lesson #4:
Download a new program DrScheme and have fun.
Choose the program language - Teaching language - Intermediate Student.
Play a little bit with the basic function in the down part. DrSchem the begining.
How to designe the program.
Next step is to try out the uper part.
Write:
(define(twice x)
(* 2 x))
(twice 5)
(* 2 (+ 4 6))
(+ 1 2 3)
and check out function like Step , Check Syntax, Run, Stop.
If you need some help click on help.
Other examples:
(sqrt (+ (expt 10 2) (expt 15 2)))
or
(< (expt 2 10) (expt 10 2))
or
(max (expt 2 10) (expt 10 2))
or
(list (expt 2 10) (expt 10 2))
Lesson #5:
Examples:
... result=188.4
... result= (list 3 -4)
... result= 5
Results:
2
5
(list 1 2 3 4)
(list 1 3 4 5)
(list 1 3 4 5)
(list 2 1)
(list 2 1)
Lesson #5:
Examples:
Create boolean function (inbetween a b c) a<=b<=c
or
... result= true
Create numeric function which limits x to the interval from min to max
Results
19
0
100
Results
-2
5
HW task!
-create a function (last L) returning the last element of the list L.. DONT USE THE REVERSE FUNCTION
Lesson #6:
Examples:
(define-struct ware (name price amount)) (define depot (list (make-ware "Coke" 12 100) (make-ware "Pepsi" 11 200) (make-ware "Sprite" 10 300))) (define (find-price name L) (cond [(empty? L) 0] [(string=? name (ware-name (car L))) (ware-price (car L))] [else (find-price name (cdr L))] )) ;;Create a function named depot-value which calculates the global value of all wares ;;in the depot (define (depot-value L) (cond [(empty? L) 0] [else (+ (global-price (car L)) (depot-value (cdr L)))] )) (define (global-price W) (* (ware-price W) (ware-amount W))) (find-price "Coke" depot) (depot-value depot)Results
12
6400
HW task!
-create a function
; create function (bill-price order), the order is a list of lists ; (("apple" 2) ("tomato" 1)) - single sublist for every ware, contains the name ans the ; number of pieces purchasedLesson #7:
Examples:
(define (findsecond Max SecMax L) (cond [(empty? L) SecMax] [(<= Max (car L)) (findsecond (car L) Max (cdr L))] [(<= (car L) SecMax) (findsecond Max SecMax (cdr L))] [else (findsecond Max (car L) (cdr L))] )) (define (max-second L) (findsecond (max (car L) (second L)) (min (car L) (second L)) (cdr (cdr L)))) (max-second '(15 10 20 17 8)) ;;quickest solution for lazy students (second (quicksort '(15 10 20 17 8) >))Results
17
17
(define (nearest_to_avg L) (cond [(empty? L) #f] [else (nearest (car L) (avg L) (cdr L))] )) (define (avg L) (/(sum L) (length L))) (define (sum L) (cond [(empty? L) 0] [else (+ (car L) (sum(cdr L)))] )) (define (nearest Candidate Target L) (cond [(empty? L) Candidate] [(< (abs (- Candidate Target)) (abs (- (car L) Target))) (nearest Candidate Target (cdr L))] [else (nearest (car L) Target (cdr L))] )) (nearest_to_avg '(6 10 15 5))Results
10
HW task!
quick sort for the last task...
Demo exam:
1.
Create function (f a b) evaluateing
((a/b) + (b/a)) /(a*b)
2.
Create function (select n L) selecting n/th element
from list L (if n> length (L) return #f)
3.
We have following definition of department:
(define-stuct dept (name employees))
where department name is a string and employees is a list of strings containing names of employees working in the departmet. Create function (isemployee name department) which returns #t if department contains employee with the given name.
Results:
;1 Task
(define (f a b)
(/ (+ (/ a b) (/ a b)) (* a b)))
(f 4 2)
;should be 0.5
;2 Task
(define (select n L)
(cond
[(empty? L) "the number is not in the list"]
[(> n (length L)) ((length L))]
[(<= n 1) n) (car L)]
[else (select (- n 1) (cdr L))]
))
(select 7 '( 2 3 4 5 6 7))
;3 Task
(define-struct dept (name employees))
(define (isempoyee name department)
(searchstring name ( dept-employees department)))
(define (searchstring s L)
(cond
[(emty? L)#f]
[(string=? s (car L)) #t]
[else (searchstring s (cdr))]
))